package com.pg.auth.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.pg.auth.model.dto.syslogin.LoginReq;
import com.pg.auth.model.dto.syslogin.LoginResp;
import com.pg.auth.service.SysAuthService;
import com.pg.base.vo.LoginVO;
import com.pg.base.vo.ResultVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Objects;
import java.util.concurrent.TimeUnit;


/**
 * 组织管理
 * @author : huhuaping
 * @Date : 2024-06-06 19:55:33
 */

@Slf4j
@RequestMapping("/auth")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LoginController {

    private final SysAuthService sysAuthService;

    @PostMapping("login")
    public  ResultVO<LoginVO> userLogin(@RequestBody LoginReq req, HttpServletRequest request ) {

        // 设置域名
        //req.setDomain(ClifeUtil.getDomain(request));
        LoginVO loginVO = sysAuthService.login(req);
        return ResultVO.instance(loginVO);
    }

    @GetMapping("logout")
    public Object logout(String device) {
        //authSerivce.logout(device);
        return ResultVO.instance();
    }

//    @PostMapping("changeLoginOrg")
//    public LoginResp changeLoginOrg(@RequestBody UserOrgResp userOrgResp) {
//        ClifeAssert.bizError(Objects.isNull(userOrgResp.getOrgId()), ErrorCodeEnum.PARAM_ERROR);
//        LoginVO loginUser= LoginUtils.getLoginUser();
//        ClifeAssert.bizError(Objects.isNull(loginUser), ErrorCodeEnum.USER_NOT_EXISTS);
//        log.info("changeLoginOrg旧用户【{}】", JSON.toJSONString(loginUser));
//        loginUser.setOrgId(userOrgResp.getOrgId());
//        authAppService.setOrgAndRole(loginUser);
//        LoginVO newLoginUser= LoginUtils.getLoginUser();
//        log.info("changeLoginOrg新用户【{}】", JSON.toJSONString(newLoginUser));
//        LoginResp resp=new LoginResp();
//        resp.setNickName(newLoginUser.getNickName());
//        resp.setToken(StpUtil.getTokenValue());
//        return resp;
//    }
//
//    @PostMapping("create")
//    public Object create(@RequestBody UserCreateReq req) {
//        authSerivce.create(req);
//        return ResultVO.instance();
//    }
//
//    @PostMapping("safeCheck")
//    public Object safeCheck(@RequestBody @Validated SafeCheckReq req) {
//        return authSerivce.safeCheck(req);
//    }
//
//    @PostMapping("changePassword")
//    public Object changePassword(@RequestBody ChangePasswordReq req) {
//        authSerivce.changePassword(req);
//        return ResultVO.instance();
//    }
//
//    /**
//     * 清空密码错误次数
//     *
//     * @param req
//     * @return
//     */
//    @PostMapping("cleanPasswordErrorCount")
//    public Object update(@RequestBody LoginReq req) {
//        String passwordCountKey = RedisConstant.getPasswordCountKey(req.getUsername(), req.getUserType());
//        RedisClient.getRedisUtil().delete(passwordCountKey);
//        return null;
//    }
//
//    @PostMapping("sendSMSCode")
//    public ResultVO sendSMSCode(@RequestBody @Valid SendSMSCodeReq req) {
//        authSerivce.sendSMSCode(req);
//        return ResultVO.instance();
//    }
//
//
//    /**
//     * 家属登录
//     * @param familyLoginReq
//     */
//    @PostMapping(value = "/ignore/familyLogin")
//    public LoginResp familylogin(@RequestBody @Validated FamilyLoginReq familyLoginReq) {
//
//        return authSerivce.familylogin(familyLoginReq);
//    }
//
//    @PostMapping("sendMQTest")
//    public ResultVO sendMQTest() {
//
//        //使用模板封装数据
//        String content = MQMessageTemplate.smsTemplate(String.valueOf(System.currentTimeMillis()), Lists.newArrayList("15019241131"));
//
//        //发送到MQ
//        MQClient.custom().syncSend(MQTopicConstant.MSG_NOTICE, content);
//
//        return ResultVO.instance();
//    }
//
//    @PostMapping("getVerificationCode")
//    public void getCode(@RequestBody @Valid VerificationCodeReq req, HttpServletResponse response, HttpServletRequest request) throws IOException {
//        HttpSession session = request.getSession();
//        //利用图片工具生成图片,第一个参数是生成的验证码,第二个参数是生成的图片
//        Object[] objs = ClifeVerificationCodeUtil.createImage();
//        //将验证码存入redis
//        RedisClient.getRedisUtil().setEx(RedisConstant.getVerificationSmsCodeKey(req.getVerificationCodeType(), req.getMobile()), (String) objs[0], 60, TimeUnit.SECONDS);
//        //禁止图像缓存
//        response.setHeader("Pragma", "no-cache");
//        response.setHeader("Cache-Control", "no-cache");
//        response.setDateHeader("Expires", 0);
//        //将图片输出给浏览器
//        BufferedImage image = (BufferedImage) objs[1];
//        response.setContentType("image/png");
//        OutputStream os = response.getOutputStream();
//        ImageIO.write(image, "png", os);
//    }
//
//    @PostMapping("test")
//    public void test(){
//
//
//        String tokanValue1 = RedisClient.getRedisUtil().getTokenValue("fee97645-7a2d-48f7-b6ba-ccfd6f48c051");
//
//        LoginUserVO aa = RedisClient.getRedisUtil().getLoginUser(tokanValue1);
//        System.out.println(tokanValue1);
//        System.out.println(JSON.toJSONString(aa));
//    }
//
//    /**
//     * 数字乡村--登陆验证
//     * @param req
//     * @return
//     */
//    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
//    @ResponseBody
//    public Object userLogin(@RequestBody LoginUserReq req){
//
//        LoginResp loginResp = authAppService.userLogin(req.getToken(), req.getType());
//        return loginResp;
//    }
}
